package com.jianshuai.service.impl;

import com.jianshuai.domain.Floor;
import com.jianshuai.dto.Devices;
import com.jianshuai.dto.FloorDto;
import com.jianshuai.feign.DeviceFeignClient;
import com.jianshuai.mapper.BuildMapper;
import com.jianshuai.mapper.FloorMapper;
import com.jianshuai.service.IBuildService;
import com.jianshuai.domain.Device;
import com.jianshuai.dto.Floors;
import com.jianshuai.dto.House;
import com.jianshuai.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BuildServiceImpl implements IBuildService {

    @Autowired
    private BuildMapper buildMapper;

    @Autowired
    private FloorMapper floorMapper;

    @Autowired
    private DeviceFeignClient deviceFeignClient;


    @Override
    public JSONResult<House> getDeviceListByBuildId(int id) {
        House house = buildMapper.getBuildId(id);
        List<Floors> floors = floorMapper.getFloors(id);

        // 根据楼栋获取楼栋中所有的楼层
        List<Floor> floor = floorMapper.getBuildId(id);
        // 根据楼栋id获取所有的设备信息
        List<Device> devices = deviceFeignClient.getByBuildId((long) id);


        // 这是所有的Devices的信息
        List<Devices> devicesList = new ArrayList<>();
        for (Device d : devices){
            Devices devices1 = new Devices(d.getName(), Math.toIntExact(d.getType()));
            devicesList.add(devices1);
        }

        System.out.println("查询出每栋楼中的设备总数::=====::" + devices);
        // 把device转成dto的类(devices) key为
        Map<Long, Devices> map = new HashMap<>();
        for (int i = 0; i < devices.size(); i++) {
            map.put(devices.get(i).getFloorId(),devicesList.get(i));
        }

        // 遍历楼层的信息并且把楼层的id放入集合
        Long[] floorId = new Long[floor.size()];
        for (int i = 0; i < floor.size(); i++) {
            floorId[i] = floor.get(i).getId();
        }

        for (int i = 0; i < floors.size(); i++){
            List<Devices> ds = new ArrayList<>();
            Long cou = floorId[i];
            for (Map.Entry<Long, Devices> entry : map.entrySet()) {
                 if (entry.getKey() == cou){
                     ds.add(entry.getValue());
                 }
            }
            floors.get(i).setDeviceList(ds);
        }


        house.setFloorList(floors);
        return JSONResult.success(house);
    }

    @Override
    public JSONResult getDeviceListByDeviceType(Long type) {
        List<Device> device = deviceFeignClient.getDeviceListByDeviceType(type);
        System.out.println("查询出的devices的对象为:++++++====::" + device);

        // 创建一个list集合把存放Device的dto
        ArrayList<Devices> devices = new ArrayList<>();
        for (int i = 0; i < device.size(); i++) {
            Devices devices1 = new Devices(device.get(i).getName(), Math.toIntExact(device.get(i).getType()));
            devices.add(devices1);
        }


        return JSONResult.success(devices);
    }
}
